﻿using System.Collections.Generic;
using System.Linq;
using NLite.Collections;
using NLite.Persistence;
using NUnit.Framework;

namespace NLite.NHPersistenceTest
{
    [TestFixture]
    public class QueryTest:TestBase
    {
      

        [Test]
        public void test_Get()
        {
            UserInfo user = new UserInfo()
            {
                Name = "TestName",
                Age = 100
            };
            repository.SaveOrUpdate(user);

            var user2 = query.FirstOrDefault(p=>p.Id == user.Id);
            Assert.AreEqual(user, user2);
            repository.Delete(user.Id);
        }

        [Test]
        public void test_FindAll()
        {
            for (int i = 0; i < 10;i++)
            {
                UserInfo user = new UserInfo()
                {
                    Name = "TestName"+('A'+i%32),
                    Age = i
                };
                repository.SaveOrUpdate(user);
            }

            var list=query.Where(u => u.Age >= 5);
            Assert.AreEqual(5, list.Count());
            list.ForEach(u => repository.Delete(u.Id));
        }

        [Test]
        public void test_EagerFetch()
        {
            for (int i = 0; i < 10; i++)
            {
                UserInfo user = new UserInfo()
                {
                    Name = "TestName" + ('A' + i % 32),
                    Age = i,
                    Books = new List<BookInfo>()
                };

                for (int j = 0; j < 3; j++)
                {
                    user.Books.Add(new BookInfo() { User = user });
                }
                repository.SaveOrUpdate(user);
            }

            var list = query.Fetch(u => u.Books).Where(u => u.Age >= 5).Select(u => u).ToArray();
            //这儿不能直接用Count，而是要先执行后再用Count;
            Assert.AreEqual(5, list.Count());
           // list.ForEach(u => repository.Delete(u.Id));
        }
    }
}
